<?php

require_once dirname(__FILE__).'/../lib/unidadGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/unidadGeneratorHelper.class.php';

/**
 * unidad actions.
 *
 * @package    sistema_estadisticas
 * @subpackage unidad
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class unidadActions extends autoUnidadActions
{

	public function execute($request)
	{
		if (!$request->isXmlHttpRequest())
		{
			$modulo_id = $request->getUrlParameter('modulo_id');
			$malla_id = $request->getUrlParameter('malla_id');

			if (!ModuloTable::getInstance()->hasAccess($modulo_id, $this->getUser()->getUserId()))
				$this->forward('login', 'secure');

			$this->getContext()->getRouting()->setDefaultParameter('modulo_id', $modulo_id);
			$this->getContext()->getRouting()->setDefaultParameter('malla_id', $malla_id);

			if ($id = $request->getUrlParameter('id'))
				$this->getContext()->getRouting()->setDefaultParameter('id', $id);
		}

		return parent::execute($request);
	}

	public function executeIndex(sfWebRequest $request)
	{
		parent::executeIndex($request);

		$modulo = ModuloTable::getInstance()->find($request->getParameter('modulo_id'));
		$this->getUser()->setFlash('info', sprintf('Módulo %d - %s', $modulo->numero, $modulo->titulo), false);
	}

	public function executeNew(sfWebRequest $request)
	{
		$modulo = ModuloTable::getInstance()->findOneById($request->getParameter('modulo_id'));

		$this->unidad = new Unidad();
		$this->unidad->setModulo($modulo);

		$this->form = new UnidadForm($this->unidad);
	}

	public function executeCreate(sfWebRequest $request)
	{
		$modulo = ModuloTable::getInstance()->findOneById($request->getParameter('modulo_id'));
		
		$this->unidad = new Unidad();
		$this->unidad->setModulo($modulo);

		$this->form = new UnidadForm($this->unidad);

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->unidad = UnidadTable::getInstance()->findByModuloId($request->getParameter('modulo_id'), $request->getParameter('id'));
		$this->form = new UnidadForm($this->unidad);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->unidad = UnidadTable::getInstance()->findByModuloId($request->getParameter('modulo_id'), $request->getParameter('id'));
		$this->form = new UnidadForm($this->unidad);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	public function executeShow(sfWebRequest $request)
	{
		parent::executeShow($request);
		$table = UnidadTable::getInstance();

		$this->prerequisitos = $table->getPrerequisitoById($this->unidad->getId());
		$this->corequisitos = $table->getCorequisitoById($this->unidad->getId());
	}

	public function executeBuscar(sfWebRequest $request)
	{
		$dato = $request->getParameter('UnidadNombre');
		$carrera = $request->getParameter('Carrera');
		$json_data = array();

		if(strlen($dato) >= 3)
		{
			$resultado = UnidadTable::getInstance()->findByCarreraId($carrera, $dato);

			foreach ($resultado as $unidad)
				$json_data[] = array('id' => $unidad[0], 'titulo' => $unidad[1] . ' - Módulo ' . $unidad[2]);
		}

		return $this->renderText(json_encode($json_data));
	}

	protected function buildQuery()
	{
		$query = parent::buildQuery();
		$query->addWhere('modulo_id = ?', $this->getRequest()->getUrlParameter('modulo_id'));

		return $query;
	}

	public function executeCompare(sfWebRequest $request)
	{
		parent::executeCompare($request);

		$this->current_relations = array();
		$this->previous_relations = array();

		$table = UnidadPrerequisitoTable::getInstance();

		$this->current_relations['UnidadPrerequisito'] = $table->getCurrentVersion($this->current->id);
		$this->previous_relations['UnidadPrerequisito'] = $table->getVersion($this->previous->id, $this->previous->version);

		$table = UnidadCorequisitoTable::getInstance();
		$this->current_relations['UnidadCorequisito'] = $table->getCurrentVersion($this->current->id);
		$this->previous_relations['UnidadCorequisito'] = $table->getVersion($this->previous->id, $this->previous->version);
	}

	public function executeDeleteVersion(sfWebRequest $request)
	{
		$request->checkCSRFProtection();

		$id = $request->getParameter('id');
		$version = $request->getParameter('version');

		$table = UnidadTable::getInstance();

		try
		{
			$table->deleteVersions($id, $version);
			$this->getUser()->setFlash('notice','La versión seleccionada se ha borrado correctamente.');
		}
		catch (Exception $e)
		{
			$this->getUser()->setFlash('error', 'Se ha producido un error al borrar la versión seleccionada.');
		}

		$this->redirect('@' . $this->helper->getUrlForAction('historic?id=') . $id);
	}

	public function executeBatchDeleteVersion(sfWebRequest $request)
	{
		$request->checkCSRFProtection();

		$versions = $request->getParameter('versions');
		$id = $request->getParameter('id');

		$table = UnidadTable::getInstance();

		try
		{
			$table->deleteVersions($id, $versions);
			$this->getUser()->setFlash('notice', 'Las versiones seleccionadas se han eliminado con correctamente.');
		}
		catch (Exception $e)
		{
			$this->getUser()->setFlash('error', 'Se ha producido un error al borrar las versiones seleccionadas.');
			throw $e;
		}

		$this->redirect('@' . $this->helper->getUrlForAction('historic?id=') . $id);
	}

}
